package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.engine.CurtainEngine;
import com.ciban.smarthome.module.net.protocol.Protocol;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.entity.Equip_Curtain;
import com.ciban.smarthome.module.net.communication.Communicate.ConnListener;
import com.ciban.smarthome.module.ui.adapter.CurtainListLvAdapter;

public class E_CurtainDetailsActivity extends BaseActivity implements
		OnClickListener {
	private static final int INTENT_CURTAIN_ADD = 100;
	private static final int INTENT_CURTAIN_EDIT = 101;

	private RelativeLayout rl_content;
	private ImageView iv_status;
	private TextView tv_name;
	private RadioGroup rg_status;
	private RadioButton rb_close, rb_stop, rb_open;
	private ImageView iv_add;
	private LinearLayout ll_mask;

	private ArrayList<Equip_Curtain> curtainList;
	private Equip_Curtain selCurtain;
	private CurtainListLvAdapter equListadapter;


	private CurtainEngine engine;
	private int equip_type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_equip_curtain_details);

		initData();

		initViews();
	}

	private void initData() {
		String area_id = getIntent().getStringExtra("area_id");
		equip_type =getIntent().getIntExtra("type", 0);

		LogUtil.info("当前窗帘详情传值：area_id->"+area_id+"  type->"+ equip_type);

		engine=new CurtainEngine(this,area_id, equip_type);
		curtainList=engine.getCurtainList();
	}


	private void initViews() {
		tv_name = ((TextView) findViewById(R.id.title));
		tv_name.setText("窗帘");
		iv_add = ((ImageView) findViewById(R.id.title_add));
		iv_add.setOnClickListener(this);
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);

		rl_content = (RelativeLayout) findViewById(R.id.curtain_details_content);
		iv_status = (ImageView) findViewById(R.id.curtain_details_iv_status);
		rg_status = (RadioGroup) findViewById(R.id.curtain_details_rg_status);
		rb_close = (RadioButton) findViewById(R.id.curtain_details_rb_close);
		rb_stop = (RadioButton) findViewById(R.id.curtain_details_rb_stop);
		rb_open = (RadioButton) findViewById(R.id.curtain_details_rb_open);

		ll_mask = (LinearLayout) findViewById(R.id.curtain_details_mask);

		setListeners();
	}

	private void setListeners() {
		ll_mask.setOnClickListener(this);

		rg_status.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				String cmd;
				switch (checkedId) {
				case R.id.curtain_details_rb_close:
					iv_status.setImageResource(R.mipmap.curtain_off);
					engine.sendCmd(Protocol.COMMON_CLOSE, selCurtain, new ConnListener() {
						@Override
						public void onSuccessed(String result) {

						}

						@Override
						public void onFailed(String err) {

						}
					});
					break;
				case R.id.curtain_details_rb_stop:
					iv_status.setImageResource(R.mipmap.curtain_stop);
					engine.sendCmd(Protocol.COMMON_STOP, selCurtain, new ConnListener() {
						@Override
						public void onSuccessed(String result) {

						}

						@Override
						public void onFailed(String err) {

						}
					});
					break;
				case R.id.curtain_details_rb_open:
					iv_status.setImageResource(R.mipmap.curtain_on);
					engine.sendCmd(Protocol.COMMON_OPEN, selCurtain, new ConnListener() {
						@Override
						public void onSuccessed(String result) {

						}

						@Override
						public void onFailed(String err) {

						}
					});
					break;
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.title_add:
			showMenuWin(iv_add);
			break;
		case R.id.curtain_details_mask:
			displayShort("请先选择设备");
			break;
		}
	}

	@Override
	protected void clickChooseEquipment() {
		if (curtainList.size() < 1) {
			displayShort("当前没有设备，请先添加");
			return;
		}

		if (equListadapter == null)
			equListadapter = new CurtainListLvAdapter(this, curtainList);
		else
			equListadapter.notifyDataSetChanged();
		showLvDialog(equListadapter, "窗帘列表", null, false);
		menuPopWin.dismiss();
	}

	@Override
	protected void clickAddEquipment() {
		super.clickAddEquipment();
		menuPopWin.dismiss();
		Intent intent = new Intent(E_CurtainDetailsActivity.this,
				E_CurtainModifyActivity.class);
		intent.putExtra("type",equip_type);
		startActivityForResult(intent, INTENT_CURTAIN_ADD);
	}

	@Override
	protected void clickDelEquipment() {
		super.clickDelEquipment();
		if (selCurtain == null) {
			displayShort("请先选中设备");
			return;
		}
		menuPopWin.dismiss();
		if(engine.delCurtain(selCurtain)){
			ll_mask.setVisibility(View.VISIBLE);
			curtainList.remove(selCurtain);
			tv_name.setText("窗帘");
			selCurtain = null;
		}
	}

	@Override
	protected void clickEditEquipment() {
		super.clickEditEquipment();
		if (selCurtain == null) {
			displayShort("请先选中设备");
			return;
		}
		menuPopWin.dismiss();
		Intent intent = new Intent(E_CurtainDetailsActivity.this,
				E_CurtainModifyActivity.class);
		intent.putExtra("curtain", selCurtain);

		startActivityForResult(intent, INTENT_CURTAIN_EDIT);

	}

	@Override
	protected void lvItemClick(int position) {
		selCurtain = curtainList.get(position);

		ll_mask.setVisibility(View.GONE);
		tv_name.setText(selCurtain.getName());
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case INTENT_CURTAIN_ADD:
			engine.refreshCurtainList();
			break;
		case INTENT_CURTAIN_EDIT:
			if (resultCode == RESULT_OK) {
				engine.refreshCurtainList();
				selCurtain = (Equip_Curtain) data.getExtras().get("curtain");
			}
			break;
		}
	}

}
